/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.zk.common;

import com.timesoft.common.message.MessageResources;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;

/**
 *
 * @author sorasaks
 */
public class ResourceUtil extends MessageResources {

    private static Logger LOG = Logger.getLogger(ResourceUtil.class);

    public static String getLabel(String key) {
        ResourceBundle resources = null;
        String s = null;
        try {
            Session session = Executions.getCurrent().getSession();
//            LOG.info("Locale 111111 >>>>>>>>>>>>>>>>>>>>> ="+session.getAttribute(Attributes.PREFERRED_LOCALE));

            resources = ResourceBundle.getBundle(bundle, (Locale) session.getAttribute(Attributes.PREFERRED_LOCALE));
            s = resources.getString(key);
        } catch (Exception e) {
            resources = ResourceBundle.getBundle(bundle, Locale.getDefault());
            try {
                s = resources.getString(key);
            } catch (Exception e1) {
                s = key;
            }
        }

        return s != null ? s : key;
    }

    public static String getLabel(String key, Object[] obj) {
        ResourceBundle resources = null;
        String s = null;
        try {
            Session session = Executions.getCurrent().getSession();
            //   LOG.info("Locale 22222 >>>>>>>>>>>>>>>>>>>>> ="+session.getAttribute(Attributes.PREFERRED_LOCALE));
            resources = ResourceBundle.getBundle(bundle, (Locale) session.getAttribute(Attributes.PREFERRED_LOCALE));
            s = resources.getString(key);
        } catch (Exception e) {
            resources = ResourceBundle.getBundle(bundle, Locale.getDefault());
            try {
                s = resources.getString(key);
            } catch (Exception e1) {
                s = key;
            }
        }

        return (s != null && !s.equals(key)) ? MessageFormat.format(s, obj) : key;
    }
}
